package com.ntschy.web.controller.bt;


import com.ntschy.bt.entity.dto.BtQuestionDTO;
import com.ntschy.bt.entity.dto.BtQuestionPageDTO;
import com.ntschy.bt.entity.mapstruct.BtQuestionMapstruct;
import com.ntschy.bt.service.IBtQuestionService;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.utils.file.DownloadFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;

/**
 * <p>
 * 问卷调查 前端控制器
 * </p>
 *
 * @author
 * @since 2021-1-12
 */
@RestController
@RequestMapping("/bt/question")
@Api(tags = "问卷调查接口")
@Validated
public class BtQuestionController extends BaseController {

    @Autowired
    private IBtQuestionService questionService;

    // mapstruct 转化接口类
    @Autowired
    private BtQuestionMapstruct questionMapstruct;


    // * ================================================基础增删改查BEGIN============================================= *

    /**
     *! 查询问卷调查列表
     *
     * @param questionPageDTO
     * @return
     */
    @ApiOperation(value = "查询问卷列表",
            notes = "<span style='color:red'>描述：</span>用来查询所有问卷信息")
    @ApiImplicitParam(name = "questionPageDTO", value = "当前页的值", required = true, dataType = "BtQuestionPageDTO")
    @PostMapping("/pageQuestion")
    public AjaxResult pageQuestion(@RequestBody BtQuestionPageDTO questionPageDTO){
        System.out.println("hi");
        return toAjax(questionService.pageQuestion(questionPageDTO),"查询失败");
    }


    /**
     *! 新增问卷
     *
     * @param questionDTO
     * @return
     */
    @ApiOperation(value = "新增问卷",
            notes = "<span style='color:red'>描述：</span>用来新增问卷")
    @ApiImplicitParam(name = "questionDTO", value = "问卷的 dto 实体类", required = true, dataType = "BtQuestionDTO")
    @PostMapping("/addSaveSaveQuestion")
    public AjaxResult addSaveSaveQuestion(@RequestBody BtQuestionDTO questionDTO){

        return toAjax(questionService.addSaveSaveQuestion(questionDTO), "新增成功", "新增失败");
    }

    /**
     *! 更新问卷
     *
     * @param questionDTO
     * @return
     */
    @ApiOperation(value = "更新问卷",
            notes = "<span style='color:red'>描述：</span>用来更新问卷")
    @ApiImplicitParam(name = "questionDTO", value = "问卷的 dto 实体类", required = true, dataType = "BtQuestionDTO")
    @PostMapping("/updateQuestion")
    public AjaxResult updateQuestion(@RequestBody BtQuestionDTO questionDTO){

        return toAjax(questionService.updateQuestion(questionDTO), "更新成功", "更新失败");
    }

    /**
     *! 查询问卷详情
     *
     * @param questionId 问卷 id
     * @return
     */
    @ApiOperation(value = "查询问卷详情",
            notes = "<span style='color:red'>描述：</span>用来查询问卷详情")
    @ApiImplicitParam(name = "questionId", value = "问卷ID", required = true, dataType = "String")
    @GetMapping("/fetchQuestion")
    public AjaxResult fetchQuestion(@RequestParam("id") String questionId){
        return toAjax(questionService.fetchQuestion(questionId), "新增失败");
    }

    /**
     *! 关闭问卷
     *
     * @param questionId 问卷 id
     * @return
     */
    @ApiOperation(value = "关闭问卷",
            notes = "<span style='color:red'>描述：</span>用来关闭问卷")
    @ApiImplicitParam(name = "questionId", value = "问卷ID", required = true, dataType = "String")
    @GetMapping("/updateCloseQuestion")
    public AjaxResult updateCloseQuestion(@RequestParam("id") String questionId){
        return toAjax(questionService.updateCloseQuestion(questionId), "关闭问卷成功", "关闭失败");
    }

    /**
     *! 删除问卷
     *
     * @param questionId 问卷 id
     * @return
     */
    @ApiOperation(value = "删除问卷",
            notes = "<span style='color:red'>描述：</span>用来删除问卷")
    @ApiImplicitParam(name = "questionId", value = "问卷ID", required = true, dataType = "String")
    @GetMapping("/deleteQuestion")
    public AjaxResult deleteQuestion(@RequestParam("id") String questionId){
        return toAjax(questionService.deleteQuestion(questionId), "删除问卷成功", "删除失败");
    }

    /**
     *! 统计问卷
     *
     * @param id 问卷主表 id
     * @return
     */
    @ApiOperation(value = "统计问卷",
            notes = "<span style='color:red'>描述：</span>用来统计问卷")
    @ApiImplicitParam(name = "questionId", value = "问卷ID", required = true, dataType = "String")
    @GetMapping("/statisticalQuestion")
    public AjaxResult statisticalQuestion(@RequestParam("id") String id){
        return toAjax(questionService.statisticalQuestion(id),"查询失败");
    }

    // * =================================================基础增删改查END============================================== *

    // /**
    //  *! 导出问卷
    //  *
    //  * @param id 问卷主表 id
    //  * @return
    //  */
    // @ApiOperation(value = "导出问卷",
    //         notes = "<span style='color:red'>描述：</span>用来导出问卷")
    // @ApiImplicitParam(name = "id", value = "问卷ID", required = true, dataType = "String")
    // @GetMapping(value = "/exportQuestion")
    // public AjaxResult exportQuestion(@RequestParam("id") String id,
    //                                  HttpServletResponse response
    //                                  )  {
    //     String str = "";
    //
    //
    //     String filename = "hello.txt";
    //     String filepath = "C:\\Users\\Think\\Pictures\\Saved Pictures\\hello.txt";
    //     // 如果文件名不为空，则进行下载
    //     if (filename != null) {
    //         File file = new File(filepath);
    //         // 如果文件存在，则进行下载
    //         if (file.exists()) {
    //             // 配置文件下载
    //             response.setHeader("content-type", "application/octet-stream");
    //             response.setContentType("application/octet-stream");
    //             // 下载文件能正常显示中文
    //             response.setHeader("Content-Disposition", "attachment;filename=" + filename);
    //             // 实现文件下载
    //             byte[] buffer = new byte[1024];
    //             FileInputStream fis = null;
    //             BufferedInputStream bis = null;
    //             try {
    //                 fis = new FileInputStream(file);
    //                 bis = new BufferedInputStream(fis);
    //                 OutputStream os = response.getOutputStream();
    //                 int i = bis.read(buffer);
    //                 while (i != -1) {
    //                     os.write(buffer, 0, i);
    //                     i = bis.read(buffer);
    //                 }
    //                 System.out.println("Download  successfully!");
    //                 str = "successfully";
    //
    //             } catch (Exception e) {
    //                 System.out.println("Download  failed!");
    //                 str = "failed";
    //
    //             } finally {
    //                 if (bis != null) {
    //                     try {
    //                         bis.close();
    //                     } catch (IOException e) {
    //                         e.printStackTrace();
    //                     }
    //                 }
    //                 if (fis != null) {
    //                     try {
    //                         fis.close();
    //                     } catch (IOException e) {
    //                         e.printStackTrace();
    //                     }
    //                 }
    //             }
    //         }
    //     }
    //     return toAjax(str,"查询失败");
    //
    // }

}
